package restful

import (
	"context"
	"vblog/apps/blog"

	"github.com/infraboard/mcube/client/rest"
)

var _ blog.Service = &BlogClient{}

type BlogClient struct {
	conn *rest.RESTClient
}

func (c *BlogClient) QueryBlog(ctx context.Context, in *blog.QueryBlogRequest) (*blog.BlogSet, error) {
	return nil, nil
}

// v1版本
// func (c *BlogClient) DescribeBlog(ctx context.Context, in *blog.DescribeBlogRequest) (*blog.Blog, error) {
// 	req, err := http.NewRequest("GET", c.conn.Address+"/vblog/api/v1/blogs/"+in.Id, nil)
// 	if err != nil {
// 		return nil, err
// 	}
// 	req.AddCookie(&http.Cookie{Name: "token", Value: c.conn.Token})
// 	resp, err := c.conn.client.Do(req)
// 	if err != nil {
// 		return nil, err
// 	}

// 	respBytes, err := io.ReadAll(resp.Body)
// 	if err != nil {
// 		return nil, err
// 	}
// 	defer resp.Body.Close()
// 	ins := blog.NewBlog(blog.NewCreateBlogRequest())
// 	// 反序列化
// 	err = json.Unmarshal(respBytes, ins)
// 	if err != nil {
// 		return nil, err
// 	}
// 	// fmt.Println(string(respBytes))
// 	return ins, nil
// }

// v2 版本
func (c *BlogClient) DescribeBlog(ctx context.Context, in *blog.DescribeBlogRequest) (*blog.Blog, error) {
	ins := blog.NewBlog(blog.NewCreateBlogRequest())
	err := c.conn.
		Get("/vblog/api/v1/blogs/").
		Suffix(in.Id).
		Do(ctx).
		Into(ins)
	if err != nil {
		return nil, err
	}
	return ins, nil
}

func (c *BlogClient) CreateBlog(ctx context.Context, in *blog.CreateBlogRequest) (*blog.Blog, error) {
	return nil, nil
}
func (c *BlogClient) UpdateBlog(ctx context.Context, in *blog.UpdateBlogRequest) (*blog.Blog, error) {
	return nil, nil
}
func (c *BlogClient) DeleteBlog(ctx context.Context, in *blog.DeleteBlogRequest) (*blog.Blog, error) {
	return nil, nil
}
